/**
 * author      : dormi330
 * date        : 2018/6/28
 * project     : mybatis
 * description : 订单controller
 */

package com.deep.trade.adaptor.provided.rest;

import com.deep.trade.application.OrderServiceFacade;
import com.deep.trade.common.HttpResonsePayload;
import com.deep.trade.infrastructure.utils.CommonUtils;
import com.deep.trade.order.query.OutDtoOrderDetail;
import com.deep.trade.order.query.OutDtoTradeOrder;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.deep.trade.common.TradeException;
import com.deep.trade.order.InDtoAuditOrder;
import com.deep.trade.order.InDtoCreateOrder;
import com.deep.trade.order.InDtoPayTradeOrder;

import static org.springframework.util.MimeTypeUtils.APPLICATION_JSON_VALUE;


@Controller
@RequestMapping(value = "/order")
public class OrderController {

    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    @Autowired
    private OrderServiceFacade orderServiceFacade;

    /**
     * 创建订单
     *
     * @param inDtoCreateOrder 输入参数
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/", method = RequestMethod.POST, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
    public HttpResonsePayload placeOrder(@RequestBody InDtoCreateOrder inDtoCreateOrder) {
        logger.info("{}, {}", CommonUtils.currentClassAndMethod(), inDtoCreateOrder);

        try {
            orderServiceFacade.placeOrder(inDtoCreateOrder);
            return HttpResonsePayload.happy(true);
        } catch (Exception ex) {
            ex.printStackTrace();
            return HttpResonsePayload.unhappy("1", ex.getMessage());

        }
    }

    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public HttpResonsePayload tradeOrderListOfMember(
            @RequestParam String tenantCode,
            @RequestParam String memberId,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
            @RequestParam(required = false, defaultValue = "10") int pageSize) {
        logger.info("{}, tenantCode={}, memberId={}, pageSize={}, pageIndex={}", CommonUtils.currentClassAndMethod(), tenantCode, memberId);
        try {
            PageInfo<OutDtoTradeOrder> pageInfo = orderServiceFacade.tradeOrderListOfMember(tenantCode, memberId, pageNum, pageSize);
            return HttpResonsePayload.happy(pageInfo);
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.toString());
            return HttpResonsePayload.unhappy("1", ex.getMessage());
        }
    }

    @ResponseBody
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public HttpResonsePayload tradeOrderDetail(
            @RequestParam String tenantCode,
            @RequestParam("id") String orderSummaryId) {
        logger.info("{}, tenantCode={}, orderSummaryId={}", CommonUtils.currentClassAndMethod(), tenantCode, orderSummaryId);
        try {
            OutDtoOrderDetail outDtoOrderDetail = orderServiceFacade.tradeOrderDetail(tenantCode, orderSummaryId);
            return HttpResonsePayload.happy(outDtoOrderDetail);
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.toString());
            String errMsg = ex instanceof TradeException ? ex.getMessage() : "some thing bad happen";
            return HttpResonsePayload.unhappy("1", errMsg);
        }
    }

    @ResponseBody
    @RequestMapping(value = "/audit", method = RequestMethod.POST)
    public HttpResonsePayload auditTradeOrder(@RequestBody InDtoAuditOrder inDtoAuditOrder) {
        logger.info("{}, inDtoAuditOrder={}", CommonUtils.currentClassAndMethod(), inDtoAuditOrder);
        try {
            orderServiceFacade.auditTradeOrder(inDtoAuditOrder);
            return HttpResonsePayload.happy();
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.toString());
            String errMsg = ex instanceof TradeException ? ex.getMessage() : "some thing bad happen";
            return HttpResonsePayload.unhappy("1", errMsg);
        }
    }

    @ResponseBody
    @RequestMapping(value = "/pay", method = RequestMethod.POST)
    public HttpResonsePayload payTradeOrder(@RequestBody InDtoPayTradeOrder inDtoPayTradeOrder) {
        logger.info("{}, inDtoPayTradeOrder={}", CommonUtils.currentClassAndMethod(), inDtoPayTradeOrder);
        try {
            orderServiceFacade.payTradeOrder(inDtoPayTradeOrder);
            return HttpResonsePayload.happy();
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.toString());
            String errMsg = ex instanceof TradeException ? ex.getMessage() : "some thing bad happen";
            return HttpResonsePayload.unhappy("1", errMsg);
        }
    }


    @ResponseBody
    @RequestMapping(value = "/mgmt/list", method = RequestMethod.GET)
    public HttpResonsePayload tradeOrderListOfTenant(
            @RequestParam String tenantCode,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
            @RequestParam(required = false, defaultValue = "10") int pageSize) {
        logger.info("{}, tenantCode={}, pageSize={}, pageIndex={}", CommonUtils.currentClassAndMethod(), tenantCode);
        try {
            PageInfo<OutDtoTradeOrder> pageInfo = orderServiceFacade.tradeOrderListOfTenant(tenantCode, pageNum, pageSize);
            return HttpResonsePayload.happy(pageInfo);
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.toString());
            String errMsg = ex instanceof TradeException ? ex.getMessage() : "some thing bad happen";
            return HttpResonsePayload.unhappy("1", errMsg);
        }
    }
}
